<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\AuctionBidModel;
use App\Models\AuctionModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Show;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AuctionBidController extends AdminController
{
    protected $title = '竞价记录';

    public function __construct()
    {
        $this->title .= '- 拍卖商品名称：' . request()->get('title');
    }

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    protected function grid(): NGrid
    {
        $auctionId = (int)request()->get('auctionId');
        if ($auctionId === 0) {
            throw new Exception('暂无记录');
        }

        $grid = new NGrid(new AuctionBidModel);
        $grid->model()->with(['bidder', 'auction'])->where('auction_id', $auctionId)->orderByDesc('id');
        $grid->column('id', __('ID'))->sortable();
        $grid->column('auction.start_price', __('auction.startPrice'));
        $grid->column('auction.inc', __('auction.inc'));
        $grid->column('bidder.user_nickname', __('auction.bidder'));
        $grid->column('last_round_price', __('auction.last_round_price'))->display(function () {
            if ((int)$this->num === 1) return $this->auction->start_price;
            else return bcadd($this->auction->start_price, bcmul($this->auction->inc, $this->num - 2, 2), 2);
        });
        $grid->column('bid_round_price', __('auction.bid_round_price'))->display(function () {
            return bcadd($this->auction->start_price, bcmul($this->auction->inc, $this->num - 1, 2), 2);
        });
        $grid->column('is_last', __('是否中拍'))->using(['0' => '否', '1' => '是']);
        return $grid;
    }
}
